<?php
/**
 * FlashMessages view helper
 * application/modules/admin/views/helpers/FlashMessages.php
 *
 * This helper creates an easy method to return groupings of
 * flash messages by status.
 *
 * @author Aaron Bach <bachya1208[at]googlemail.com
 * @license Free to use - no strings.
 * @source http://www.zfsnippets.com/snippets/view/id/37
 */
class Zend_View_Helper_FlashMessages extends Zend_View_Helper_Abstract
{
    /**
     * @var array
     * Default options
     */
    private $options=array(
        'groupType' =>'default',    //'status' or 'default'
        'translate'	=>false,		    //translate page title
    );

    /**
     * flashMessages function.
     *
     * Takes a specially formatted array of flash messages and prepares them
     * for output.
     *
     * SAMPLE INPUT (in, say, a controller):
     *    $this->_flashMessenger->addMessage(array('message' => 'Success message #1', 'status' => 'success'));
     *    $this->_flashMessenger->addMessage(array('message' => 'Error message #1', 'status' => 'error'));
     *    $this->_flashMessenger->addMessage(array('message' => 'Warning message #1', 'status' => 'warning'));
     *    $this->_flashMessenger->addMessage(array('message' => 'Success message #2', 'status' => 'success'));
     *
     * SAMPLE OUTPUT (in a view):
     *    <div class="success">
     *        <ul>
     *            <li>Success message #1</li>
     *            <li>Success message #2</li>
     *        </ul>
     *    </div>
     *    <div class="error">Error message #1</div>
     *    <div class="warning">Warning message #2</div>
     *
     * @access public
     * @param $translator An optional instance of Zend_Translate
     * @return string HTML of output messages
     */
    public function flashMessages(array $options=array())
    {
        $options = array_merge($this->options, $options);

        // Set up some variables, including the retrieval of all flash messages.
        $messageHelper      = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
        $previousMessages   = $messageHelper->getMessages();
        $currentMessages    = $messageHelper->getCurrentMessages();

        $allMessages        = array_merge((array)$currentMessages, (array)$previousMessages);
        //print_r($allMessages);exit;
        $allMessagesNew = array();
        foreach ($allMessages as $key=>$flashMessage) {
            $allMessagesNew[$flashMessage['type'].$flashMessage['message']] = $flashMessage;
        }
        $allMessages = array_values($allMessagesNew);
        unset($allMessagesNew);
        //print_r($allMessages);exit;

        $flashMessages      = array();

        // If there are no messages, don't bother with this whole process.
        if (count($allMessages) > 0)
        {
            // This chunk of code takes the messages (formatted as in the above sample
            // input) and puts them into an array of the form:
            //    Array(
            //        [status1] => Array(
            //            [0] => "Message 1"
            //            [1] => "Message 2"
            //        ),
            //        [status2] => Array(
            //            [0] => "Message 1"
            //            [1] => "Message 2"
            //        )
            //        ....
            //    )
            foreach ($allMessages as $key=>$message)
            {
                if ($options['groupType'] == 'status') {
                    if (!array_key_exists($message['type'], $flashMessages)) {
                        $flashMessages[$message['type']] = array();
                    }

                    if ($options['translate']) {
                        $flashMessages[$message['type']][] = $this->view->translate($message['message']);
                    } else {
                        $flashMessages[$message['type']][] = $message['message'];
                    }
                } else {
                    if ($options['translate']) {
                        $message['message'] = $this->view->translate($message['message']);
                        $flashMessages[$key] = $message;
                    } else {
                        $flashMessages[$key] = $message;
                    }

                }

            }
            //print_r($flashMessages);exit;

            return $this->view->partial('_common/flashMessages.phtml', 'default', array('flashMessages' => $flashMessages, 'groupType'=>$options['groupType']));

            // Return the final HTML string to use.
        }

    }
}